var m = Object.defineProperty;
var y = (p, o, t) =>
  o in p
    ? m(p, o, { enumerable: !0, configurable: !0, writable: !0, value: t })
    : (p[o] = t);
var x = (p, o, t) => (y(p, typeof o != 'symbol' ? o + '' : o, t), t);
import { _Renderer as T } from './Renderer.js';
import { _TextRenderer as w } from './TextRenderer.js';
import { _defaults as I } from './defaults.js';
import { unescape as R } from './helpers.js';
class u {
  constructor(o) {
    x(this, 'options');
    x(this, 'renderer');
    x(this, 'textRenderer');
    (this.options = o || I),
      (this.options.renderer = this.options.renderer || new T()),
      (this.renderer = this.options.renderer),
      (this.renderer.options = this.options),
      (this.textRenderer = new w());
  }
  /**
   * Static Parse Method
   */
  static parse(o, t) {
    return new u(t).parse(o);
  }
  /**
   * Static Parse Inline Method
   */
  static parseInline(o, t) {
    return new u(t).parseInline(o);
  }
  /**
   * Parse Loop
   */
  parse(o, t = !0) {
    let n = '';
    for (let c = 0; c < o.length; c++) {
      const s = o[c];
      if (
        this.options.extensions &&
        this.options.extensions.renderers &&
        this.options.extensions.renderers[s.type]
      ) {
        const e = s,
          i = this.options.extensions.renderers[e.type].call(
            { parser: this },
            e,
          );
        if (
          i !== !1 ||
          ![
            'space',
            'hr',
            'heading',
            'code',
            'table',
            'blockquote',
            'list',
            'html',
            'paragraph',
            'text',
          ].includes(e.type)
        ) {
          n += i || '';
          continue;
        }
      }
      switch (s.type) {
        case 'space':
          continue;
        case 'hr': {
          n += this.renderer.hr();
          continue;
        }
        case 'heading': {
          const e = s;
          n += this.renderer.heading(
            this.parseInline(e.tokens),
            e.depth,
            R(this.parseInline(e.tokens, this.textRenderer)),
          );
          continue;
        }
        case 'code': {
          const e = s;
          n += this.renderer.code(e.text, e.lang, !!e.escaped);
          continue;
        }
        case 'table': {
          const e = s;
          let i = '',
            l = '';
          for (let a = 0; a < e.header.length; a++)
            l += this.renderer.tablecell(this.parseInline(e.header[a].tokens), {
              header: !0,
              align: e.align[a],
            });
          i += this.renderer.tablerow(l);
          let k = '';
          for (let a = 0; a < e.rows.length; a++) {
            const h = e.rows[a];
            l = '';
            for (let r = 0; r < h.length; r++)
              l += this.renderer.tablecell(this.parseInline(h[r].tokens), {
                header: !1,
                align: e.align[r],
              });
            k += this.renderer.tablerow(l);
          }
          n += this.renderer.table(i, k);
          continue;
        }
        case 'blockquote': {
          const e = s,
            i = this.parse(e.tokens);
          n += this.renderer.blockquote(i);
          continue;
        }
        case 'list': {
          const e = s,
            i = e.ordered,
            l = e.start,
            k = e.loose;
          let a = '';
          for (let h = 0; h < e.items.length; h++) {
            const r = e.items[h],
              b = r.checked,
              f = r.task;
            let g = '';
            if (r.task) {
              const d = this.renderer.checkbox(!!b);
              k
                ? r.tokens.length > 0 && r.tokens[0].type === 'paragraph'
                  ? ((r.tokens[0].text = d + ' ' + r.tokens[0].text),
                    r.tokens[0].tokens &&
                      r.tokens[0].tokens.length > 0 &&
                      r.tokens[0].tokens[0].type === 'text' &&
                      (r.tokens[0].tokens[0].text =
                        d + ' ' + r.tokens[0].tokens[0].text))
                  : r.tokens.unshift({
                      type: 'text',
                      text: d + ' ',
                    })
                : (g += d + ' ');
            }
            (g += this.parse(r.tokens, k)),
              (a += this.renderer.listitem(g, f, !!b));
          }
          n += this.renderer.list(a, i, l);
          continue;
        }
        case 'html': {
          const e = s;
          n += this.renderer.html(e.text, e.block);
          continue;
        }
        case 'paragraph': {
          const e = s;
          n += this.renderer.paragraph(this.parseInline(e.tokens));
          continue;
        }
        case 'text': {
          let e = s,
            i = e.tokens ? this.parseInline(e.tokens) : e.text;
          for (; c + 1 < o.length && o[c + 1].type === 'text'; )
            (e = o[++c]),
              (i +=
                `
` + (e.tokens ? this.parseInline(e.tokens) : e.text));
          n += t ? this.renderer.paragraph(i) : i;
          continue;
        }
        default: {
          const e = 'Token with "' + s.type + '" type was not found.';
          if (this.options.silent) return console.error(e), '';
          throw new Error(e);
        }
      }
    }
    return n;
  }
  /**
   * Parse Inline Tokens
   */
  parseInline(o, t) {
    t = t || this.renderer;
    let n = '';
    for (let c = 0; c < o.length; c++) {
      const s = o[c];
      if (
        this.options.extensions &&
        this.options.extensions.renderers &&
        this.options.extensions.renderers[s.type]
      ) {
        const e = this.options.extensions.renderers[s.type].call(
          { parser: this },
          s,
        );
        if (
          e !== !1 ||
          ![
            'escape',
            'html',
            'link',
            'image',
            'strong',
            'em',
            'codespan',
            'br',
            'del',
            'text',
          ].includes(s.type)
        ) {
          n += e || '';
          continue;
        }
      }
      switch (s.type) {
        case 'escape': {
          const e = s;
          n += t.text(e.text);
          break;
        }
        case 'html': {
          const e = s;
          n += t.html(e.text);
          break;
        }
        case 'link': {
          const e = s;
          n += t.link(e.href, e.title, this.parseInline(e.tokens, t));
          break;
        }
        case 'image': {
          const e = s;
          n += t.image(e.href, e.title, e.text);
          break;
        }
        case 'strong': {
          const e = s;
          n += t.strong(this.parseInline(e.tokens, t));
          break;
        }
        case 'em': {
          const e = s;
          n += t.em(this.parseInline(e.tokens, t));
          break;
        }
        case 'codespan': {
          const e = s;
          n += t.codespan(e.text);
          break;
        }
        case 'br': {
          n += t.br();
          break;
        }
        case 'del': {
          const e = s;
          n += t.del(this.parseInline(e.tokens, t));
          break;
        }
        case 'text': {
          const e = s;
          n += t.text(e.text);
          break;
        }
        default: {
          const e = 'Token with "' + s.type + '" type was not found.';
          if (this.options.silent) return console.error(e), '';
          throw new Error(e);
        }
      }
    }
    return n;
  }
}
export { u as _Parser };
